<?php
    $this->beginWidget('zii.widgets.CPortlet', array(
        'title' => 'Upload documents',
        'id' => 'upload_document',
    ));
?>
<div id="upload_zone">

</div>
<?php $this->endWidget(); ?>

<?php
$DocumentCreateUrl = CHtml::normalizeUrl(array('documents/create'));

$script =<<<EOD
function createUploader(){
            var uploader = new qq.FileUploader({
                element: $('div#upload_zone')[0],
                action: '$DocumentCreateUrl',
                params:{
                    'foreign_key':$id,
                    'for_model':'cargo'
                },
                onComplete: function(id, fileName, responseJSON){
                    $.fn.yiiGridView.update('document-grid');
                },
                onCancel: function(id, fileName){
                    $.fn.yiiGridView.update('document-grid');
                },
                allowedExtensions: ['jpg','jpeg','png','gif','pdf','tif','doc','txt']
            });
        }
createUploader();
EOD;
$cs = Yii::app()->getClientScript();
$publishurl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.vendors.ajax-uploader.client'));
$cs->registerScriptFile($publishurl.'/fileuploader.js');
$cs->registerCssFile($publishurl.'/fileuploader.css');
$cs->registerScript('cargo_upload#tab',$script);
?>